package Test07;

//多线程

/*
需求：某电影院目前正在上映国产大片，共有100张票，而它有3个窗口卖票，请设计一个程序模拟该电影院卖票
 */
public class Test09 {
    public static void main(String[] args) {

        MyThread09 t1 = new MyThread09();
        MyThread09 t2 = new MyThread09();
        MyThread09 t3 = new MyThread09();

        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");

        t1.start();
        t2.start();
        t3.start();
    }

}

class MyThread09 extends Thread {

    //表示这个类的所有对象，都共享ticket数据
    static int ticket = 0;

    /*//锁对象，一定要是唯一的
    static Object obj = new Object();*/

    @Override
    public void run() {
        while (true) {
            //同步代码块
            synchronized (MyThread09.class) {
                if (ticket < 100) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ticket++;
                    System.out.println(getName() + "正在卖第" + ticket + "张票");
                } else {
                    break;
                }
            }
        }
    }
}
